﻿package com.tvein.sanguo.swc.event
{
	//flash
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	//A3D
	//custom
	import com.tvein.sanguo.swc.common.OverLay;
	import com.tvein.sanguo.swc.common.SwcEvent;
	import com.tvein.sanguo.swc.common.MP3Click;
//
	public class EventShell extends MovieClip
	{
		public static const NAME:String = "EventShell";
		
		private var eventType:String;
		private var overLay:OverLay;
		private var gameConfig:Array;
		private var loader:Loader;
		
		public function EventShell(configArray:Array)
		{
			stage?init(null):addEventListener(Event.ADDED_TO_STAGE, init);
			
			gameConfig = configArray;
		}

		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE , init);
			addEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			stage.addEventListener(Event.RESIZE, _resizeUI);
			
			overLay = new OverLay();
			this.addChild(overLay);
			this.setChildIndex(overLay, 0);
			
			talk.mcDialog.txtName.background = true;
			
			plot.visible = false;
			talk.visible = false;
			talk.buttonMode = true;
			plot.buttonMode = true;

			talk.addEventListener(MouseEvent.CLICK, onClick);
			
			_resizeUI();
		}

		public function hidden():void
		{
			talk.visible = false;
			plot.visible = false;
			
			overLay.width = overLay.height = 0;
			eventType = "";
		}
		
		public function initMovie(Swf:String):void
		{
			eventType = "Plot";
			
			plot.visible = false;
			
			for (var i:int = plot.numChildren - 1; i >= 0; i--)
			{
				plot.removeChildAt(0); 
			}
			
			loadSWF(Swf);
		}
		
		private function loadSWF(Swf:String):void
		{		
			loader = new Loader();
			
			loader.load(new URLRequest(gameConfig["srcServer"] + "/SanGuo/event/" + Swf));
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, _onSWFLoad);
		}

		private function _onSWFLoad(evt:Event):void
		{			
			loader.removeEventListener(Event.COMPLETE, _onSWFLoad);
			
			dispatchEvent(new SwcEvent(NAME, ["music", "mute"], true));
			
			loader.content.addEventListener(Event.ENTER_FRAME, _onSWFPlay);
			loader.content.addEventListener(MouseEvent.CLICK, _onSWFPlaySkip);
						
			plot.addChild(loader.content);
			plot.visible = true;
			
			_resizeUI();
		}
		
		private function _onSWFPlaySkip(evt:MouseEvent):void
		{
			var soundEffect:MP3Click = new MP3Click();
			soundEffect.playback(gameConfig["effectVol"] *.01);
			
			loader.content.removeEventListener(Event.ENTER_FRAME, _onSWFPlay);
			loader.content.removeEventListener(MouseEvent.CLICK, _onSWFPlaySkip);
				
			loader.unloadAndStop();
			dispatchEvent(new SwcEvent(NAME, ["play"], true));
			dispatchEvent(new SwcEvent(NAME, ["music", "resume"], true));
		}

		private function _onSWFPlay(evt:Event):void
		{
			if (evt.target.currentFrame == evt.target.totalFrames - 1)
			{
				loader.content.removeEventListener(Event.ENTER_FRAME, _onSWFPlay);
				loader.content.removeEventListener(MouseEvent.CLICK, _onSWFPlaySkip);
				
				loader.unloadAndStop();
				dispatchEvent(new SwcEvent(NAME, ["play"], true));
				dispatchEvent(new SwcEvent(NAME, ["music", "resume"], true));
			}
		}
		
		public function initTips(role:Object, say:String, media:String):void
		{
			eventType = "Tips";
			
			plot.visible = false;
			
			for (var i:int = plot.numChildren - 1; i >= 0; i--)
			{
				plot.removeChildAt(0); 
			}
			
			talk.visible = false;
			
			for (var j:int = talk.mcRole.numChildren - 1; j >= 0; j--)
			{
				talk.mcRole.removeChildAt(0); 
			}
			
			loadFace(int(role.image));
			loadMedia(media);
			
			talk.mcDialog.txtName.text = role.name;
			talk.mcDialog.txtSay.htmlText = say;
		}
		
		private function loadMedia(media:String):void
		{		
			loader = new Loader();
			loader.load(new URLRequest(gameConfig["srcServer"] + "/SanGuo/event/" + media));
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, _onMediaLoad);
		}

		private function _onMediaLoad(evt:Event):void
		{
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, _onMediaLoad);
			
			plot.addChild(evt.target.content);
			
			plot.visible = true;
			
			_resizeUI();
		}
		
		public function initSpeak(role:Object, say:String):void
		{
			eventType = "Talk";
			
			talk.visible = false;
			
			for (var i:int = talk.mcRole.numChildren - 1; i >= 0; i--)
			{
				talk.mcRole.removeChildAt(0); 
			}
			
			loadFace(int(role.image));
			
			talk.mcDialog.txtName.text = role.name;
			talk.mcDialog.txtSay.htmlText = say;
		}

		private function loadFace(ImageID:int):void
		{		
			loader = new Loader();
			loader.load(new URLRequest(gameConfig["srcServer"] + "/SanGuo/role/bust/" + ImageID + ".png"));
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, _onFaceLoad);
		}

		private function _onFaceLoad(evt:Event):void
		{
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, _onFaceLoad);
			
			talk.mcRole.addChild(evt.target.content);
			talk.visible = true;
			
			_resizeUI();
		}

		private function _onRemoved(evt:Event):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.removeEventListener(Event.RESIZE, _resizeUI);
		}

		private function _resizeUI(evt:Event = null):void
		{
			var i:int = 400;
			var j:int = 500;
			var m:Number, n:Number;
			
			this.x = this.y = talk.x = talk.y = plot.x = plot.y = overLay.x = overLay.y = 0;
			
			this.width = overLay.width = stage.stageWidth;
			this.height = overLay.height = stage.stageHeight;
			
			if (this.width < i)
			{
				this.width = overLay.width =  i;
			}
			
			if (this.height < j)
			{
				this.height = overLay.height =  j;
			}
			
			switch(eventType)
			{
				case "Tips":
					talk.x = this.width - talk.width;
					talk.y = this.height - talk.height - 60;
					plot.x = (this.width - plot.width) * .5;
					plot.y = (this.height - plot.height) * .3;
					break;
				case "Talk":
					talk.x = this.width - talk.width;
					talk.y = this.height - talk.height - 60;
					break;
				case "Plot":
					plot.x = (this.width - plot.width) * .5;
					plot.y = (this.height - plot.height) * .3;
					break;
				default:
					overLay.width = overLay.height = 0;
			}
		}

		private function onClick(evt:MouseEvent):void
		{
			var soundEffect:MP3Click = new MP3Click();
			soundEffect.playback(gameConfig["effectVol"] *.01);
			
			talk.visible = false;
			plot.visible = false;
			dispatchEvent(new SwcEvent(NAME, ["play"], true));
		}
	}
}